import 'package:flutter/material.dart';
import 'package:flutter_fronter_service/api/goodsApi.dart';
import 'package:get/get.dart';
import '../model/Category.dart';
import '../widget/PageViewSlid.dart';

class CatePage extends StatefulWidget {
  const CatePage({super.key});

  @override
  State<CatePage> createState() => _CatePageState();
}

class _CatePageState extends State<CatePage> {
  List<String> imageList = [
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/6d202d8e-bb47-4f92-9523-f32ab65754f4.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/dfc11bb0-4af5-4e9b-9458-99f615cc685a.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/e83efb1b-309c-46f7-98a3-f1fefa694338.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/4a79180a-1a5a-4042-8a77-4db0b9c800a8.jpg",
  ];

  int _selectedCategoryIndex = 0;
  RxList<dynamic> categories = <dynamic>[].obs;
  bool isLoading = true;  // 是否正在加载

  Future<void> getCateList() async {
    var res = await getCateGoodsApi();
    if (res != null && res["data"] != null) {
      categories.value = res["data"];
      if (categories.isNotEmpty) {
        isLoading = false;  // 数据加载完毕，更新加载状态
      }
    }
    setState(() {});  // 重新build
  }

  @override
  void initState() {
    super.initState();
    getCateList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("商品分类"),
      ),
      body: isLoading ? Center(child: CircularProgressIndicator()) : buildBody(), // 根据加载状态决定显示内容
    );
  }

  Widget buildBody() {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: ListView.separated(
            itemCount: categories.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(
                    categories[index]['parentName'].toString(),
                  style: TextStyle(
                    color: _selectedCategoryIndex == index
                        ? Colors.green
                        : Colors.black,
                  ),),
                onTap: () {
                  setState(() {
                    _selectedCategoryIndex = index;
                  });
                },
              );
            },
            separatorBuilder: (context, index) => Divider(),
          ),
        ),
        Expanded(
          flex: 3,
          child: ListView(
            children: [
              Container(
                height: 80,
                child: PageViewSlid(
                  duration: 3500,
                  imageList: imageList,
                  isUseIndexDots: true,
                  height: 80,
                ),
              ),
              Obx(() {
                var children = categories.isNotEmpty ? categories[_selectedCategoryIndex]['children'] : [];
                return Container(
                  height: 1800,
                  child: ListView.builder(
                    itemCount: children.length,
                    itemBuilder: (context, index) {
                      var subCategory = children[index];
                      return ExpansionTile(
                        title: Text(subCategory['name']),
                        children: subCategory['goodsVos'].map<Widget>((item) {
                          return GestureDetector(onTap: () {
                            //通过Getx跳转
                            Get.toNamed("/goodsDetail",arguments: {"id":item["id"]});
                            },child:ListTile(
                              title: Text(item['name'], maxLines: 1, overflow: TextOverflow.ellipsis),
                              subtitle: Text("￥${item['price']}", style: TextStyle(color: Colors.red)),
                              leading: Image.network(item['picture'], width: 50, height: 50),
                          ));
                        }).toList(),
                      );
                    },
                  ),
                );
              })
            ],
          ),

        ),
      ],
    );
  }
}

